package com.example.project.service.impl;

import com.example.project.common.MyTool;
import com.example.project.dao.ExternalUnitDao;
import com.example.project.dto.ExternalUnitBinPageDTO;
import com.example.project.dto.SafetyTrainBinDTO;
import com.example.project.entity.ExternalUnit;
import com.example.project.entity.ExternalUnitBin;
import com.example.project.dao.ExternalUnitBinDao;
import com.example.project.entity.SafetyInspe;
import com.example.project.entity.SafetyTrainBin;
import com.example.project.service.ExternalUnitBinService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (ExternalUnitBin)表服务实现类
 *
 * @author makejava
 * @since 2024-02-28 09:20:14
 */
@Service("externalUnitBinService")
public class ExternalUnitBinServiceImpl implements ExternalUnitBinService {
    @Resource
    private ExternalUnitBinDao externalUnitBinDao;
    @Resource
    private ExternalUnitDao externalUnitDao;

    private MyTool myTool = new MyTool();

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ExternalUnitBin queryById(Integer id) {
        return this.externalUnitBinDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param externalUnit 筛选条件
     * @param pageRequest  分页对象
     * @param startTime
     * @param endTime
     * @return 查询结果
     */
    @Override
    public Page<ExternalUnitBinPageDTO> queryByPage(ExternalUnit externalUnit, PageRequest pageRequest, Date startTime, Date endTime) {
        //删除到期的数据
        this.deleteAllOldTime();

        long total = this.externalUnitDao.count(externalUnit);
        List<ExternalUnitBinPageDTO> externalUnitBinPageDTOS = new ArrayList<>();

        List<ExternalUnit> externalUnits = this.externalUnitDao.queryAllByLimit(externalUnit, pageRequest,startTime,endTime);
        for(ExternalUnit e:externalUnits){
            ExternalUnitBinPageDTO dto = new ExternalUnitBinPageDTO();
            dto.setExternalUnit(e);
            dto.setExternalUnitBin(this.externalUnitBinDao.queryByExternalUnitId(e.getId()));
            externalUnitBinPageDTOS.add(dto);
        }
        return new PageImpl<>(externalUnitBinPageDTOS, pageRequest, total);
    }

    /**
     * 删除所有到期的时间
     * */
    private void deleteAllOldTime() {
        List<ExternalUnitBin> list = this.externalUnitBinDao.queryAll();
        for (ExternalUnitBin e:list){
            Date date = myTool.dateSomeNOw(e.getBinTime(),e.getDeleteTime());
            Date date1 = myTool.dateNow();
            if (date1.before(date)) {
                //"date1 在 date 之后"

            } else if (date1.after(date)) {
                //"date1 在 date 之前"
                this.deleteById1(e.getId());
            } else {
                this.deleteById1(e.getId());
                //"date1 和 date 相等"
            }
        }
    }

    /**
     * 新增数据
     *
     * @param externalUnitBin 实例对象
     * @return 实例对象
     */
    @Override
    public ExternalUnitBin insert(ExternalUnitBin externalUnitBin) {
        //生成删除时间
        externalUnitBin.setDeleteTime(myTool.dateNow());
        this.externalUnitBinDao.insert(externalUnitBin);
        return externalUnitBin;
    }

    /**
     * 修改数据
     *
     * @param externalUnitBin 实例对象
     * @return 实例对象
     */
    @Override
    public ExternalUnitBin update(ExternalUnitBin externalUnitBin) {
        this.externalUnitBinDao.update(externalUnitBin);
        return this.queryById(externalUnitBin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.externalUnitBinDao.deleteById(id) > 0;
    }
    /**
     * 恢复
     * */
    @Override
    public Boolean update1(ExternalUnitBin externalUnitBin) {
        //修改管理表中的内容
        ExternalUnit externalUnit  = this.externalUnitDao.queryById(externalUnitBin.getExternalUnitId());
        externalUnit.setRecycle(1);
        this.externalUnitDao.update(externalUnit);
        //彻底删除
        return this.deleteById(externalUnitBin.getId());
    }
    /**
     * 删除数据
     * */
    @Override
    public Boolean deleteById1(Integer id) {
        //删除管理表中的数据
        ExternalUnitBin externalUnitBin = this.externalUnitBinDao.queryById(id);
        this.externalUnitDao.deleteById(externalUnitBin.getExternalUnitId());
        return this.deleteById(id);
    }

    @Override
    public Boolean deleteById2(SafetyTrainBinDTO data) {
        boolean flog1 = false;
        boolean flog2 = false;
        if (data.getBinIds() != null){
            List<Integer> listSTB = myTool.myToList(data.getBinIds());
            flog1 = this.externalUnitBinDao.deleteByIdAll(listSTB)>0;
        }

        if (data.getIds() != null){
            List<Integer> listST = myTool.myToList(data.getIds());
            flog2 = this.externalUnitDao.deleteByIdAll(listST)>0;
        }

        return flog2&&flog1;
    }

    @Override
    public Boolean update2(SafetyTrainBinDTO data) {
        boolean flog1 = false;
        boolean flog2 = false;
        if (data.getBinIds() != null){
            List<Integer> listSTB = myTool.myToList(data.getBinIds());
            flog1 = this.externalUnitBinDao.deleteByIdAll(listSTB)>0;
        }

        if (data.getIds() != null){
            List<Integer> listST = myTool.myToList(data.getIds());
            flog2 = this.externalUnitDao.batchUpdate(listST)>0;
        }
        return flog2&&flog1;
    }
}
